<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['complainservice'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['complainservice'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
	$print = isset($_GET['print']) ? $_GET['print'] : 0;
	if(isset($_POST['completeBtn']) && $_POST['completeBtn'] > 0)
	{
		$updateComplete = "UPDATE randrmaster
		                      SET status = 'D',
		                          closeDate = '".date('Y-m-d')."',
		                          closeRemarks = '".$_POST['updationRemarks']."'
		                    WHERE rAndRMasterId = ".$_POST['rAndRMasterId'];
		$updateCompleteRes = mysql_query($updateComplete);
		if(!$updateCompleteRes)
		  die("Update Query Error : ". mysql_error());
		else
		  header("Location:pendingRandRList.php");
		  exit();
	}
	elseif(isset($_POST['inCompleteBtn']) && $_POST['inCompleteBtn'] > 0)
	{
		$updateInComplete = "UPDATE randrmaster
		                        SET updationDate = '".date('Y-m-d')."',
		                            updationRemarks = '".$_POST['updationRemarks']."'
		                      WHERE rAndRMasterId = ".$_POST['rAndRMasterId'];
		$updateInCompleteRes = mysql_query($updateInComplete);
		if(!$updateInCompleteRes)
		  die("Update Query Error : ". mysql_error());
		else
		  header("Location:pendingRandRList.php");
		  exit();
	}
	
	if(isset($_GET['rAndRMasterId']) && $_GET['rAndRMasterId'] > 0)
	{
	  $selRnRData = "SELECT rAndRMasterId, party.companyName AS partyName,	userName,	srNo,
	                        DATE_FORMAT(complaintDate,'%d-%m-%Y') AS complaintDate,
	                        DATE_FORMAT(targetDate,'%d-%m-%Y') AS targetDate,	supplier.companyName AS supplier,	
	                        supplier.phoneNo,supplier.mobile1,supplier.mobile2, randrmaster.address
	                   FROM randrmaster
	                   JOIN party    ON randrmaster.partyId =  party.partyId
	                   JOIN user     ON randrmaster.userId = user.userId
	                   JOIN supplier ON randrmaster.supplierId = supplier.supplierId
	                  WHERE rAndRMasterId = ".$_GET['rAndRMasterId'];
    $selRnRDataRes = mysql_query($selRnRData);
    $masterArray = array();
    $detailArray = array();
    if($masterRow = mysql_fetch_array($selRnRDataRes))
    {
      $masterArray['rAndRMasterId'] = $masterRow['rAndRMasterId'];
      $masterArray['partyName']     = $masterRow['partyName'];
      $masterArray['userName']      = $masterRow['userName'];
      $masterArray['srNo']          = $masterRow['srNo'];
      $masterArray['complaintDate'] = $masterRow['complaintDate'];
      $masterArray['targetDate']    = $masterRow['targetDate'];
      $masterArray['supplier']      = $masterRow['supplier'];
      $masterArray['phoneNo']       = $masterRow['phoneNo'].",".$masterRow['mobile1'].",".$masterRow['mobile2'];
      $masterArray['address']       = $masterRow['address'];
      
      $selRnRDtlData = "SELECT subCategoryName, itemName,	quantity,	complaintDescription
                          FROM randrdetail
                          JOIN subcategory ON randrdetail.subCategoryId = subcategory.subCategoryId
                          JOIN item        ON randrdetail.itemId = item.itemId
                         WHERE rAndRMasterId = ".$_GET['rAndRMasterId'];
      $selRnRDtlDataRes = mysql_query($selRnRDtlData);
      $count = 0;
      while($detailRow = mysql_fetch_array($selRnRDtlDataRes))
      {
      	$detailArray[$count]['subCategoryName']      = $detailRow['subCategoryName'];
      	$detailArray[$count]['itemName']             = $detailRow['itemName'];
      	$detailArray[$count]['quantity']             = $detailRow['quantity'];
      	$detailArray[$count]['complaintDescription'] = $detailRow['complaintDescription'];
      	$count++;
      }
    }
  }
  include("./bottom.php");
  $smarty->assign("masterArray",$masterArray);
  $smarty->assign("detailArray",$detailArray);
  if($print == 1)
    $smarty->display("rNrPrint.tpl");
  else
    $smarty->display("changeStatusRnR.tpl");
}
?>